﻿using CommonLib.Pattern.FSM;
using CommonLib.Util;
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;

public class EnemyFSMStateBase : FSMStateBase
{
	protected RoleCtrl Role;
	protected Collider2D target;
	protected float vigilance;

	public EnemyFSMStateBase(string _key, RoleCtrl _role , IFSM _machine, float duration = 0.02f) : base(_key, _machine, duration)
	{
		this.Role = _role;
	}
	

	protected TransitionParam GetTransistionParam(TransitionParamType type) //=> (FromState.Machine as FSMEnemy).Levles[Role.RoleEntity.CurrentStage].Params[type];
	{
		int stage = Role.RoleEntity.CurrentStage;
		FSMEnemy fSMEnemy = Machine as FSMEnemy;
		//Check stage valid
		if (stage < 0)
			return null;
		if (stage >= fSMEnemy.Levles.Count)
			stage = fSMEnemy.Levles.Count - 1;
		TransitionParam tParams = fSMEnemy.Levles[stage].Params[type];
		return tParams;
	}
	protected bool FindNearestDangrous(float vigilance, out Collider2D collider)
	{
		collider = null;
		if (Collider2DUtil.Box2DFindNearest(Role.gameObject.transform.position,
			new Vector2(Role.SubManagerMove.Collider.bounds.size.x + 2 * vigilance / 100.0f, Role.SubManagerMove.Collider.bounds.size.y + 2 * vigilance / 100.0f),
			(col) =>
			{
				if ((col.gameObject.tag == Const.Tag_Player && col.gameObject != Role.gameObject))
				{
					RoleCtrl temp = col.GetComponent<RoleCtrl>();
					return temp.RoleEntity.CurrentScore > Role.RoleEntity.CurrentScore ? true : false;
				}
				else if (col.gameObject.tag == Const.Tag_ItemEffect)
				{
					EffectItemBase effectItem = col.GetComponent<EffectItemBase>();
					return !effectItem.IsPositive;
				}
				else
					return false;
			},
			out Collider2D dangrous) == true)
		{
			collider = dangrous;
			return true;
		}
		else
			return false;

	}
	protected bool FindNearestChase(float vigilance, out Collider2D collider)
	{
		Collider2D[] Competitors = new Collider2D[0];
		Collider2DUtil.Box2DFindAll(Role.gameObject.transform.position,
			new Vector2(Role.SubManagerMove.Collider.bounds.size.x + 2 * vigilance / 100.0f, Role.SubManagerMove.Collider.bounds.size.y + 2 * vigilance / 100.0f), (col) =>
			{
				if (col.gameObject.tag == Const.Tag_Player && col.gameObject != Role.gameObject)
				{
					return true;
				}
				else
				{
					return false;
				}
			},
			out Competitors);

		collider = null;
		if (Collider2DUtil.Box2DFindNearest(Role.gameObject.transform.position,
			new Vector2(Role.SubManagerMove.Collider.bounds.size.x + 2 * vigilance / 100.0f, Role.SubManagerMove.Collider.bounds.size.y + 2 * vigilance / 100.0f),
			(col) =>
			{
				if (col.gameObject.tag == Const.Tag_Player && col.gameObject != Role.gameObject)
				{
					RoleCtrl temp = col.GetComponent<RoleCtrl>();
					if (temp.RoleEntity.CurrentScore < Role.RoleEntity.CurrentScore) {
						if (null != Competitors)
						{
							for (int index = 0; index < Competitors.Length; index++)
							{
								if (Vector3.Distance(Role.transform.position, col.transform.position) <
								 Vector3.Distance(Competitors[index].transform.position, col.transform.position))
									continue;
								else
								{
									return false;
								}
							}
						}
						return true;
					}else
						return false;
				}
				else
					return false;
			},
			out Collider2D target) == true)
		{
			collider = target;
			return true;
		}
		else
			return false;
	}
	protected bool FindNearestForward(float vigilance,out Collider2D collider)
	{
		collider = null;
		Collider2D[] Competitors = new Collider2D[0];
		Collider2DUtil.Box2DFindAll(Role.gameObject.transform.position,
			new Vector2(Role.SubManagerMove.Collider.bounds.size.x + 2 * vigilance / 100.0f, Role.SubManagerMove.Collider.bounds.size.y + 2 * vigilance / 100.0f), (col) =>
			{
				if (col.gameObject.tag == Const.Tag_Player && col.gameObject != Role.gameObject)
				{
					return true;
				}
				else
				{
					return false;
				}
			},
			out Competitors);

		if (Collider2DUtil.Box2DFindNearest(Role.gameObject.transform.position,
			new Vector2(Role.SubManagerMove.Collider.bounds.size.x + 2 * vigilance / 100.0f, Role.SubManagerMove.Collider.bounds.size.y + 2 * vigilance / 100.0f),
			(col) =>
			{
				if (col.gameObject.tag == Const.Tag_ItemEffect)
				{

					EffectItemBase temp = col.GetComponent<EffectItemBase>();
					if(temp.IsPositive)
					{
						if (null != Competitors)
						{
							for (int index = 0; index < Competitors.Length; index++)
							{
								if (Vector3.Distance(Role.transform.position, col.transform.position) <
								 Vector3.Distance(Competitors[index].transform.position, col.transform.position))
									continue;
								else
								{
									return false;
								}
							}
						}
						return true;
					}
					return false;
				}
				else
					return false;
			},
			out Collider2D target) == true)
		{
			collider = target;
			return true;
		}
		else
			return false;
	}

	public void SetTarget(Collider2D t)
	{
		this.target = t;
	}
}
